<template>
    <div>
        <div class="search">
            <input v-model="keyword" class="search-input" type="text" placeholder="输入城市名/拼音">
        </div>
        <div class="search-content" ref="search" v-show="keyword">
            <ul>
                <!-- 循环遍历list，显示搜索到的内容 -->
                <li  class="search-item border-bottom"
                    v-for="item of list"
                    :key="item.id"
                    @click="handleCityClick(item.name)"
                    >{{item.name}}
                </li>
                <!-- 优化处理：没有找到城市 -->
                <li  class="search-item border-bottom"
                    v-show="hasNoData"
                    >没有找到城市
                </li>
            </ul>
        </div>
    </div>
</template>

<script>
import Bscroll from 'better-scroll'
export default {
    name: 'CitySearch',
    props: {
        cities: Object
    },
    data () {
        return {
            keyword: '',
            list: [],
            timer: null
        }
    },
    methods: {
        handleCityClick (city) {
            this.$store.dispatch('changeCity', city)
            this.$router.push('/')
        }
    },
    computed: {
        hasNoData () {
            return !this.list.length
        }
    },
    watch: {
        keyword () {
           if (this.timer) {
                clearTimeout(this.timer)
           }
        // 优化：当搜索结果为空时，不显示搜索结果区域
           if (!this.keyword) {
                this.list = []
                return
           }
        // 当keyword改变时，搁100ms之后箭头函数开始执行
           this.timer = setTimeout(() => {
                const result = []
                // 循环cities中的内容
                for (let i in this.cities) {
                    // 将cities中的每一项的键的对应值再循环
                    this.cities[i].forEach((value) => {
                        // value中有两个字段spell和name，通过indexof判断value中是否存在keyword的值
                        if (value.spell.indexOf(this.keyword) > -1 ||
                        value.name.indexOf(this.keyword) > -1) {
                            // 如果存在则将结果存入数组中
                            result.push(value)
                        }
                    })
                }
                // 将结果赋值给list
                this.list = result
           }, 100)
        }
    },
    mounted () {
        this.scroll = new Bscroll(this.$refs.search,
        { mouseWheel: true, click: true, tap: true })
    }
}
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl'
    .search
        height: .72rem
        padding: 0 .1rem
        background: $bgColor
        .search-input
            width: 100%
            height: .62rem
            line-height: .62rem
            box-sizing: border-box
            border-radius: .06rem
            color: #666
            padding: 0 .1rem
            text-align: center
    .search-content
        z-index: 1
        position: absolute
        overflow: hidden
        top: 1.58rem
        left: 0
        right: 0
        bottom: 0
        background: #eee
        .search-item
            line-height: .62rem
            padding-left: .2rem
            background: #fff
            color: #666
</style>
